package com.stone.business.jxfa.action;
/**
 * -----计划  ACTION---
 */

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import bsh.StringUtil;

import com.stone.business.jxfa.pojo.TJxfa;
import com.stone.business.jxfa.pojo.TJxfaBz;
import com.stone.business.jxfa.pojo.TJxfaGznr;
import com.stone.business.jxfa.service.JxfaService;
import com.stone.business.plan.pojo.TPlan;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TOrgan;
import com.stone.utils.DateFormatTool;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

@SuppressWarnings("serial")
public class JxfaAction extends BaseAction{
	
	private JxfaService jxfaService;

	private TJxfa entity;
	
	private String id;
	private String status;
	private List<TJxfaBz> jxfaBzList;
	private List<TJxfaGznr> jxfaGznrList;
	private int gznrListSize;
	
	
	private String hiddenids;
	
	/**
	 * 进入月度工作计划详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initJxfaDetail() throws AppException {

		// 修改进入
		if (!"0".equals(id.toString())) {

			this.entity = (TJxfa) jxfaService.findById(Long.parseLong(id),TJxfa.class.getName());

		// 新建进入
		} else {
			
			this.entity = new TJxfa();
			this.entity.setStatus("1");
		}
		setDictionary();
		return SUCCESS;
		
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws AppException
	 */
	public String saveJxfa() throws AppException {
		

		Date confDate = DateFormatTool.formatToDate(jxfaService.getSysdate("yyyy-MM-dd HH24:mi"), true);
		
		String rtn = jxfaService.doSave(this.entity, this.jxfaBzList, this.jxfaGznrList);
		
		//setDictionary();
		
		super.setInfo(rtn);

		return SUCCESS;

	}
	
	/**
	 * 删除
	 * 
	 * @return
	 * @throws AppException
	 */
	public String deleteJxfa() throws AppException {
		
		jxfaService.doDelAll(id, TJxfa.class.getName());
		jxfaService.deleteByDHql("planid in (" + id + ")", null, TJxfaBz.class.getName());
		jxfaService.deleteByDHql("planid in (" + id + ")", null, TJxfaGznr.class.getName());
		
		return "DELETE_SUCCESS";
	}

	/**
	 * 提交 直接审核
	 * 
	 * @return
	 * @throws AppException
	 */
	public String confirmJxfa() throws AppException {
		jxfaService.updateByDHql("status = '" + super.getRequest().getParameter("flag") + "'", "id in (" + this.id + ")", null, null, TJxfa.class.getName());
		return "DELETE_SUCCESS";	
	}
	
	/**
	 * 进入详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
//	public String initDetail() throws AppException {
//	       
//		String   selectpage=super.getSearchBean().getCon1();
//
//		// 修改进入
//		if (!"0".equals(id)) {
//
//			this.entity = (TPlan) planService.findById(Long.parseLong(id),TPlan.class.getName());
//			return SUCCESS;
//
//		// 新建进入
//		} else {
//			
//			this.entity = new TPlan();
//			return SUCCESS;
//
//		}
//		
//	}

	/**
	 * 保存、审核
	 * 
	 * @return
	 * @throws AppException
	 */
//	public String save() throws AppException {
//		
//		String   selectpage=super.getSearchBean().getCon1();
//		
//		Date confDate = DateFormatTool.formatToDate(planService.getSysdate("yyyy-MM-dd HH24:mi"), true);
//		if (this.entity.getId() == 0) {
//			
//			this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
//		}
//		String rtn = planService.doSave(this.entity, this.planbzList,this.hiddenids);
//		
//		super.setInfo(rtn);
//
//		return SUCCESS;
//
//	}
	
	/**
	 * -----提交计划----
	 * 
	 * @return
	 * @throws AppException
	 */
//	public String submit() throws AppException {
//		
//		Date confDate = DateFormatTool.formatToDate(planService.getSysdate("yyyy-MM-dd HH24:mi"), true);
//		this.entity = (TPlan) planService.findById(Long.parseLong(id),TPlan.class.getName());
//		
//		String rtn = planService.doSubmit(this.entity,status);
//		super.setInfo(rtn);
//		if (status == "1"){
//			return "SUBMIT_SUCCESS";
//			
//		}else {
//			
//			return "PASS_SUCCESS";
//		}
//	}
	
	//
//	public String getGzapList(){
//		String sql = "";
//		String conditions = " where status = '9'";
//		//月度计划
//		if("1".equals(datafrom)){
//			sql  = 	"select a.id,a.teamnames,a.startdate,a.enddate,a.powerrange,a.voltagelevel,a.workcontent,a.status," +
//	   				" case when b.id is null then '0' else '1' end as ishavegzd" +
//	   				" from tab_plan a left join tab_gzrwd b" +
//	   				" on b.planid = a.id";
//		//临时安排
//		}else if("2".equals(datafrom)||"3".equals(datafrom)){
//			sql  =	"select a.id,a.bzname as teamnames,a.startdate,a.enddate,a.tdfw as powerrange,a.dydj as voltagelevel,a.gzxm as workcontent,a.status," + 
//					" case when b.id is null then '0' else '1' end as ishavegzd" + 
//					" from tab_lsap a left join tab_gzrwd b" + 
//					" on b.planid = a.id";
//		}
//
//		
//		if ("3".equals(super.getSearchBean().getCon20())) {
//			conditions += " and a.startdate - trunc(sysdate) = 2";
//		}else if("4".equals(super.getSearchBean().getCon20())){
//			conditions += " and a.startdate - trunc(sysdate) = 1";
//		}else if("5".equals(super.getSearchBean().getCon20())){
//			conditions += " and a.startdate - trunc(sysdate) = 0";
//		}
//	
//		TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
//		if (o.getOrganCode().toString().length() > 6 && !"1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) {
//			conditions += " and a.ouid = '" + o.getOrganCode() + "'";
//		}
//		//临时生产工作安排
//		if("2".equals(datafrom)){
//			conditions += " and a.state = '0'";
//		//临时管理工作安排
//		}else if("3".equals(datafrom)){
//			conditions += " and a.state = '1'";
//		}
//		
//		if("1".equals(showtype)||"4".equals(showtype)||"7".equals(showtype)){
//			conditions += " and b.id is not null";
//		}else if("2".equals(showtype)||"5".equals(showtype)||"8".equals(showtype)){
//			conditions += " and b.id is null";
//		}
//		
//		sql += conditions;
//		gzzbList = planService.findListBySql(sql);
//		
//		return "GET_LIST_SUCCESS";
//	}
	/**
	 * 获得字典项目
	 */
	private void setDictionary() {
		
		
		// 班组
		List<TOrgan> list = jxfaService.findListByHql(" from TOrgan where organNameEn like '%B' ");
		
		if ("0".equals(id)) {
			
			this.jxfaBzList = new ArrayList<TJxfaBz>();
			int i = 0;
			for (TOrgan organ : list) {
				TJxfaBz bz = new TJxfaBz();
				bz.setId(Long.parseLong("0"));
				bz.setBzid(organ.getId());
				bz.setBzname(organ.getOrganName());
				this.jxfaBzList.add(bz);
				i++;
			}
		}else{
			this.jxfaBzList = jxfaService.findListByHql("from TJxfaBz where jxfaid = '" + id + "'");
			this.jxfaGznrList = jxfaService.findListByHql("from TJxfaGznr where jxfaid = '" + id + "'");
		}
		if(this.jxfaGznrList!=null){
			this.gznrListSize = this.jxfaGznrList.size();
		}
		
		//工作内容
		//List<TJxfaGznr> templist = jxfaService.findListByHql(" from TJxfaGznr ");
		
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}


	public String getHiddenids() {
		return hiddenids;
	}

	public void setHiddenids(String hiddenids) {
		this.hiddenids = hiddenids;
	}

	public JxfaService getJxfaService() {
		return jxfaService;
	}

	public void setJxfaService(JxfaService jxfaService) {
		this.jxfaService = jxfaService;
	}

	public TJxfa getEntity() {
		return entity;
	}

	public void setEntity(TJxfa entity) {
		this.entity = entity;
	}

	public List<TJxfaBz> getJxfaBzList() {
		return jxfaBzList;
	}

	public void setJxfaBzList(List<TJxfaBz> jxfaBzList) {
		this.jxfaBzList = jxfaBzList;
	}

	public List<TJxfaGznr> getJxfaGznrList() {
		return jxfaGznrList;
	}

	public void setJxfaGznrList(List<TJxfaGznr> jxfaGznrList) {
		this.jxfaGznrList = jxfaGznrList;
	}

	public int getGznrListSize() {
		return gznrListSize;
	}

	public void setGznrListSize(int gznrListSize) {
		this.gznrListSize = gznrListSize;
	}


}
